//
//  YYSReadSaveController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/17.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSReadSaveController.h"
#import "YYSReadRecondCell.h"
#import "YYSPlayVideoController.h"
@interface YYSReadSaveController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *titlleView;
@property (nonatomic , assign) BOOL isquan;
@property (nonatomic , strong) NSMutableArray *seclecdNums;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottpmViewheight;
@property (nonatomic , strong) NSMutableArray *allNumbers;
@property (nonatomic , strong) NSMutableArray *models;
@property (weak, nonatomic) IBOutlet UIButton *quanBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleBtn;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;

@end

@implementation YYSReadSaveController
-(NSMutableArray *)seclecdNums
{
    if (_seclecdNums == nil) {
        _seclecdNums = [NSMutableArray array];
    }
    return _seclecdNums;
}
-(NSMutableArray *)models
{
    if (_models == nil) {
        _models = [NSMutableArray array];
        for (int i = 0; i<15; i++) {
            
            [_models addObject:@"123"];
        }
    }
    return _models;
}
-(NSMutableArray *)allNumbers
{
    if (_allNumbers == nil) {
        _allNumbers = [NSMutableArray array];
    }
    return _allNumbers;
}
- (IBAction)clickVideo:(UIButton *)sender {
    if (sender.tag == 100) {
        
    }else if (sender.tag == 200) {
        
    } else {
        
    }
    [UIView animateWithDuration:0.5 animations:^{
        
        view.yy_centerX = sender.yy_centerX;
    }];
}
static NSString *ID = @"YYSReadRecondCell";
UIView *view;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    label.text = self.title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
    
    view = [UIView new];
    view.backgroundColor = YYSGlobalColor;
    view.frame = CGRectMake(0, self.titlleView.yy_height - 2, self.titlleView.yy_width/3, 2);
    [self.titlleView addSubview:view];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [btn setImage:[UIImage imageNamed:@"shanchu"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    _isquan = NO;
    self.bottpmViewheight.constant = 0;
    self.bottomView.hidden = YES;
    
    if ([self.title isEqualToString:@"浏览记录"]) {
        self.topViewHeight.constant = 40;
        self.topView.hidden = NO;

    } else {
        self.topViewHeight.constant = 0;
        self.topView.hidden = YES;

    }
}
- (void)clickDelete:(UIButton *)btn
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickDelete" object:nil];
    btn.selected = !btn.selected;
    if (btn.selected) {
        _isquan = YES;
        self.bottomView.hidden = NO;
        self.bottpmViewheight.constant = 40;
    }else{
        _isquan= NO;
        self.bottomView.hidden = YES;
        self.bottpmViewheight.constant = 0;
    }
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.seclecdNums.count == 0) {
        [self.deleBtn setTitleColor:YYSLabelTextColor forState:UIControlStateNormal];
    } else {
        [self.deleBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    for (int i = 0; i<self.models.count; i++) {
        [self.allNumbers addObject:@(i)];
    }
    return self.models.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YYSReadRecondCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.isquanxian = _isquan;
    cell.ischoice = [self.seclecdNums containsObject:@(indexPath.row)];
        return cell;
}
- (IBAction)clickAll:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _seclecdNums = [NSMutableArray arrayWithArray:_allNumbers];
        [sender setTitle:@"全不选" forState:UIControlStateNormal];
    } else {
        [_seclecdNums removeAllObjects];
        [sender setTitle:@"全选" forState:UIControlStateNormal];
    }
    
    [self.tableView reloadData];
}
//点击删除
- (IBAction)clickQuxiao:(id)sender {
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isquan) {
      
//        cell.selected = !cell.selected;
        if (![self.seclecdNums containsObject:@(indexPath.row)]) {
            
           
            [self.seclecdNums addObject:@(indexPath.row)];

        } else {
         
            [self.seclecdNums removeObject:@(indexPath.row)];
        }
        [self.tableView reloadData];
    } else {
        YYSPlayVideoController *play = [[YYSPlayVideoController alloc] init];
        [self.navigationController pushViewController:play animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
